package com.boniu.startapp;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.boniu.startapp.databinding.ActivityMainBinding;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;


public class MainActivity extends AppCompatActivity {
    public static final String TAG = "asd";
    private AppCompatActivity mContext;
    RelativeLayout rlXuanfuchuang;
    RelativeLayout rlWuzhangai;
    TextView tv2,tv1;
    private boolean canOpen = true;
    private RelativeLayout rlHoutai;
    private ActivityMainBinding mBind;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBind = DataBindingUtil.setContentView(this, R.layout.activity_main);
        setContentView(R.layout.activity_main);
        mContext = this;
        rlWuzhangai = findViewById(R.id.rl_wuzhangai);
        rlXuanfuchuang = findViewById(R.id.rl_xuanfuchuang);
        tv1 = findViewById(R.id.tv_1);
        tv2= findViewById(R.id.tv_2);
        rlHoutai = ((RelativeLayout) findViewById(R.id.rl_houtai));
        initClick();
    }

    private void initClick() {
        rlXuanfuchuang.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View v) {
                AccessibilityUtils.requestXfcWindow(mContext);
            }
        });

        rlWuzhangai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AccessibilityUtils.requestWxs(mContext);
            }
        });
        rlHoutai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toSelfSetting();
            }
        });

        findViewById(R.id.btn_dingding).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(TAG, "onClick: "  + checkPackInfo("com.alibaba.android.rimet"));
                PackageManager packageManager = getPackageManager();
                Intent intent=new Intent();
                intent =packageManager.getLaunchIntentForPackage("com.alibaba.android.rimet");
                if (intent != null){

                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    Toast.makeText(mContext, "启动失败", Toast.LENGTH_SHORT).show();
                }
            }
        });

        getPkgListNew();
    }



    /**
     * 检查包是否存在
     *
     * @param packname
     * @return
     */
    private boolean checkPackInfo(String packname) {
        PackageInfo packageInfo = null;
        try {
            packageInfo = getPackageManager().getPackageInfo(packname, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return packageInfo != null;
    }

    private List<String> getPkgListNew() {
        List<String> packages = new ArrayList<String>();
        try {
            List<PackageInfo> packageInfos = getPackageManager().getInstalledPackages(PackageManager.GET_ACTIVITIES |
                    PackageManager.GET_SERVICES);
            for (PackageInfo info : packageInfos) {
                String pkg = info.packageName;
                Log.e(TAG, "getPkgListNew: " + pkg );
                packages.add(pkg);
            }
        } catch (Throwable t) {
            t.printStackTrace();;
        }
        return packages;
    }
    public void toSelfSetting() {
        Intent mIntent = new Intent();
        mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            mIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            mIntent.setData(Uri.fromParts("package", getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            mIntent.setAction(Intent.ACTION_VIEW);
            mIntent.setClassName("com.android.settings", "com.android.setting.InstalledAppDetails");
            mIntent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());
        }
        startActivity(mIntent);
    }
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onResume() {
        super.onResume();
        String manufacturer = Build.MANUFACTURER;
        if (manufacturer != null && manufacturer.length() > 0) {
            String phone_type = manufacturer.toLowerCase();
            if ("vivo".equals(phone_type)) {
                int floatPermissionStatus = AccessibilityUtils.getFloatPermissionStatus(mContext);
                tv1.setText(floatPermissionStatus == 0 ? "已开启" : "未开启");
                tv1.setTextColor(floatPermissionStatus == 0 ? getResources().getColor(R.color.yikaiqi) : getResources().getColor(R.color.weikaiqi));
            } else {
                tv1.setText(Settings.canDrawOverlays(mContext) ? "已开启" : "未开启");
                tv1.setTextColor(Settings.canDrawOverlays(mContext) ? getResources().getColor(R.color.yikaiqi) : getResources().getColor(R.color.weikaiqi));
            }
        }


        tv2.setText(AccessibilityUtils.isServiceON(mContext) ? "已开启" : "未开启");
        tv2.setTextColor(AccessibilityUtils.isServiceON(mContext) ? getResources().getColor(R.color.yikaiqi) : getResources().getColor(R.color.weikaiqi));

        if ("已开启".equals(tv1.getText().toString()) && "已开启".equals(tv2.getText().toString())) {
            setResult(RESULT_OK);
        }

    }


}